package com.ftj.bills.aspect;

import com.ftj.bills.sys.pojo.Billtype;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 21seu.ftj
 */
@Aspect
@Component
@EnableAspectJAutoProxy
public class BillTypeCacheAspect {

    /**
     * 缓存对象
     */
    private Map<String, Object> cache = new HashMap<>();

    /**
     * 缓存前缀
     */
    private static final String BILL_TYPE_CACHE_PREFIX = "billtype:";

    @Pointcut("execution(* com.ftj.bills.sys.service.impl.BilltypeServiceImpl.getById(..))")
    public void pc() {

    }

    @Around(value = "pc()")
    public Object cacheBillType(ProceedingJoinPoint point) throws Throwable {
        //得到目标方法的参数
        Object[] args = point.getArgs();
        //取出id
        Integer typeId = (Integer) args[0];
        //从缓存里面取对象
        Object obj = cache.get(BILL_TYPE_CACHE_PREFIX + typeId);
        if (null != obj) { //说明缓存里面有数据
            System.out.println("缓存里面有数据");
            return obj;
        } else {
            System.err.println("缓存里面没有数据，执行SQL查询");
            //说明缓存里没有  放行执行目标方法查询数据
            Billtype resoult = (Billtype) point.proceed();
            //放入缓存
            cache.put(BILL_TYPE_CACHE_PREFIX + resoult.getId(), resoult);
            return resoult;
        }
    }
}
